module dungeon.mover;

import nade.vector;


class Mover {
	alias void delegate(const Vector2i, const Vector2i) ArrivalDg;

	this(const Vector2i from = Vector2i.ZERO, const Vector2i to = Vector2i.ZERO)
	{
		_from = from;
		_to = to;
		_travel = 0;
	}

	void move(real distance, ArrivalDg arrived)
	{
		_travel += distance;
		if(_travel >= 1)
		{
			_travel -= 1;

			const prev = _from;
			_from = _to;

			if(arrived !is null) arrived(prev, _from);
		}
	}

	Vector2 position()
	{
		return Vector2(
			(_to.x - _from.x)*_travel + _from.x + 0.5,
			(_to.y - _from.y)*_travel + _from.y + 0.5
			);
	}

	Vector2i from() { return _from; }
	Vector2i from(Vector2i rhs) { return _from = rhs; }

	Vector2i to() { return _to; }
	Vector2i to(Vector2i rhs) { return _to = rhs; }

	real travel() { return _travel; }
	real travel(real rhs) { return _travel = rhs; }

	protected {
		Vector2i		_from;
		Vector2i		_to;
		real			_travel;
	}
}
